package com.example.myletcodelearing

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        startActivity(Intent(this,SecondActivity::class.java))
    }


    override fun onDestroy() {
        super.onDestroy()
        Log.d("tgw销毁", "onDestroy: ")
    }
}
class SecondActivity : AppCompatActivity() {
    companion object{
        lateinit var  activity:Context
    }
    @RequiresApi(Build.VERSION_CODES.Q)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        activity = this@SecondActivity

        registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks{
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityResumed(activity: Activity) {
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                Log.d("SecondActivitytgw onActivityDestroyed 销毁", "onDestroy: ")
            }

        })
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("SecondActivitytgw销毁", "onDestroy: ")
    }
}
/**
 * 其中第一个传入的是需要读写的key，第二个传入是当读取出来的数据不存在的时候默认返回的值
 */
class Preference<T>(val activity: MainActivity,private val label: String, private val default: T): ReadWriteProperty<Any?, T> {

    private val sp: SharedPreferences by lazy {
        //主要是获取SP对象,这个根据每个项目视情况获取
        activity.getSharedPreferences("data", Context.MODE_PRIVATE)
    }


    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        putSharePreferences(label, value)
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return getSharePreferences(label, default)
    }

    private fun putSharePreferences(name: String, value: T) = with(sp.edit()) {
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            is Float -> putFloat(name, value)
            else -> throw IllegalArgumentException("This type of data cannot be saved!")
        }.apply()
    }

    @Suppress("UNCHECKED_CAST")
    private fun getSharePreferences(name: String, default: T): T = with(sp) {
        val res: Any? = when (default) {
            is Long -> getLong(name, default)
            is String -> getString(name, default)
            is Int -> getInt(name, default)
            is Boolean -> getBoolean(name, default)
            is Float -> getFloat(name, default)
            else -> throw IllegalArgumentException("This type of data cannot be saved!")
        }
        return res as T
    }

}